package top.datacluster.basic.algorithm.leetcode.dynamic;

public class LeetCode1586 {

    public int maxValue(int[][] grid) {

        int[][] dp = new int[grid.length][grid[0].length];

        for (int i = 0; i < grid.length; i++){
            for (int j = 0; j < grid[0].length; j++){
                if (i == 0 && j== 0){
                    dp[i][j] = grid[i][j];
                }
                if (i == 0 && j != 0){
                    dp[i][j] = dp[i][j-1] + grid[i][j];
                }
                if (i != 0 && j == 0){
                    dp[i][j] = dp[i-1][j] + grid[i][j];
                }
                if (i != 0 && j != 0){
                    dp[i][j] = Math.max(dp[i][j-1],dp[i-1][j]) + grid[i][j];
                }
            }
        }
        return dp[grid.length - 1][grid[0].length - 1];
    }

    public static void main(String[] args) {
        int[][] test = new int[][]{{1,2,5},{3,2,1}};
        System.out.println(new LeetCode1586().maxValue(test));
    }
}
